import path from 'path';
import { utilityProcess, BrowserWindow } from 'electron';
import { showTopApplication } from '../Common';
import IpcEvent from '../../event/services';

export interface ProcessMessage {
    type: string;
}

class HttpProcess {
    #mainWindow!: BrowserWindow;
    #childProcess!: Electron.UtilityProcess;
    public start(mainWindow: BrowserWindow): void {
        this.#mainWindow = mainWindow;
        this.#childProcess = utilityProcess.fork(
            path.resolve(__dirname, './index.js')
        );
        this.listener();
    }
    public listener() {
        this.#childProcess.on('message', (value) => {
            if (value.type === 'print:file') {
                IpcEvent.sendPrintFile(value.value);
                showTopApplication(this.#mainWindow);
            }
        });
    }
}

export default new HttpProcess();
